package com.example.administrator.yuejian2.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.activity.MasterDetailActivity;
import com.example.administrator.yuejian2.bean.MasterBean;
import com.example.administrator.yuejian2.viewholder.MasterViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.example.administrator.yuejian2.other.Url.IMAGEURL;

public class MasterAdapter extends RecyclerView.Adapter{

    private Context context;
    private LayoutInflater layoutInflater;
    private List<MasterBean> list;
    public MasterAdapter(Context context)
    {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    public void getMasterList(List<MasterBean> list)
    {
            this.list=list;
            notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.master_item,parent,false);
        return new MasterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MasterViewHolder masterViewHolder=(MasterViewHolder)holder;
        final MasterBean bean=list.get(position);
        masterViewHolder.master_name.setText(bean.getMaster_name());
        masterViewHolder.master_attention.setText(bean.getMaster_attention());
        Picasso.with(context).load(IMAGEURL+bean.getMaster_pic()).into(masterViewHolder.master_pic);
        masterViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, MasterDetailActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("m_id",bean.getMaster_id());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list==null)
        {
            return 0;
        }
        else
        {
            return  list.size();
        }
    }
}
